package com.hilaver.dzmis.web.servlet.order;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Locale;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hilaver.dzmis.Constants;
import com.hilaver.dzmis.basicinfo.BiAccessoryZip;
import com.hilaver.dzmis.order.OrderZip;
import com.hilaver.dzmis.service.impl.OrderZipServiceImpl;

/**
 * Servlet implementation class for Servlet: OrderServlet
 * 
 */
public class OrderZipServlet extends
		com.hilaver.dzmis.web.servlet.AbstractBaseServlet implements
		javax.servlet.Servlet {
	static final long serialVersionUID = 1L;
	
	public final static int ACTION_GET_MAX_REFERENCE_INDEX = 10;
	
	private OrderZip orderZip;
	private OrderZipServiceImpl orderZipService;

	public OrderZipServlet() {
		super();
		this.orderZipService = new OrderZipServiceImpl();
		this.orderZipService.setRealFileDir(UPLOAD_TEMP_REAL_DIR);
		this.fullName = OrderZip.class.getName();
		this.simpleName = OrderZip.class.getSimpleName();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter writer = response.getWriter();
		try {
			String outputStr = execute(request);
			if(this.action == ACTION_EDIT){
				response.setContentType(Constants.HTML_RESPONSE_TYPE);
			}
			writer.write(outputStr);
		} catch (Exception e) {
			writer.print(e.getMessage());
			e.printStackTrace();
		}
		writer.close();
	}

	protected String execute(HttpServletRequest request) throws Exception {
		this.setAction(request);
		this.orderZipService.setLocale((Locale) request.getSession().getAttribute(
				Constants.SESSION_LOCALE));
		super.setAcUser(request, this.orderZipService);
		String rtn = "";

		switch (this.action) {
		case ACTOIN_GET_ALL_PAGINATION: // "getAllPagination";

			break;
		case ACTION_EDIT: // "edit";
			this.fillObj(request);
			this.orderZipService.edit(this.orderZip);
			break;
		case ACTION_DELETE: // "delete";
			this.setId(request);
			rtn = this.orderZipService.delete(this.id);
			break;
		case ACTION_GET: // "get one"
			this.setId(request);
			rtn = this.orderZipService.get(this.id);
			break;
		case ACTION_GET_WITH_DESCRIPTION:
			this.setId(request);
			rtn = this.orderZipService.get(this.id);
			break;
		case ACTION_GET_ALL_PAGINATION_FILTER: // "getAllPagination filter"
			this.setFilterParam(request);
			this.setPaginationParam(request);
			rtn = this.orderZipService.getAllPagination(page, sort, dir, filters);
			break;
		case ACTION_GET_ALL:
			this.setId(request);
			rtn = this.orderZipService.getAll(this.id);
			break;
		case ACTION_GET_MAX_REFERENCE_INDEX:
			rtn = this.orderZipService.getMaxReferenceIndex();
			break;
		}
		return rtn;
	}

	protected void fillObj(HttpServletRequest request) throws Exception {
		super.fillObj(request);
		this.orderZip = (OrderZip)this.obj;
		String biZipIdStr = (String)getValue(request, "biZipId");
		if(biZipIdStr != null){
			Integer biButtonId = Integer.parseInt(biZipIdStr);
			BiAccessoryZip zip = new BiAccessoryZip();
			zip.setId(biButtonId);
			this.orderZip.setBiAccessoryZip(zip);
		}
	}
	
}